﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jbe.TestSuite.Infrastructure.Interface;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.ComponentModel;
using Jbe.Composite.Extensions;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Reflection;
using Jbe.TestSuite.LogViewer.Demo.Properties;

namespace Jbe.TestSuite.LogViewer.Demo.Applications
{
    internal class LogDemoPresentationModel : PresentationModel<ILogDemoView>, IHeaderInfoProvider
    {
        private static readonly PropertyInfo GeneralOptionProperty = TypeManager.GetProperty<LogDemoPresentationModel>(x => x.GeneralOption);
        private static readonly PropertyInfo DebugOptionProperty = TypeManager.GetProperty<LogDemoPresentationModel>(x => x.DebugOption);
        private static readonly PropertyInfo TraceOptionProperty = TypeManager.GetProperty<LogDemoPresentationModel>(x => x.TraceOption);


        private bool generalOption;
        private bool debugOption;
        private bool traceOption;
        private readonly LogEntry logEntry = new LogEntry();
        private readonly DelegateCommand logCommand;


        public LogDemoPresentationModel(ILogDemoView view)
            : base(view)
        {
            logCommand = new DelegateCommand(Log);
        }


        public string HeaderInfo
        {
            get { return Resources.LogDemo; }
        }

        public ImageSource HeaderIcon
        {
            get { return new BitmapImage(ResourceService.GetPackUri("Resources/ReportEdit.ico")); }
        }

        public LogEntry LogEntry
        {
            get { return logEntry; }
        }

        public ICommand LogCommand
        {
            get { return logCommand; }
        }

        public bool GeneralOption
        {
            get { return generalOption; }
            set
            {
                if (generalOption != value)
                {
                    generalOption = value;
                    RaisePropertyChanged(GeneralOptionProperty);
                }
            }
        }

        public bool DebugOption
        {
            get { return debugOption; }
            set
            {
                if (debugOption != value)
                {
                    debugOption = value;
                    RaisePropertyChanged(DebugOptionProperty);
                }
            }
        }

        public bool TraceOption
        {
            get { return traceOption; }
            set
            {
                if (traceOption != value)
                {
                    traceOption = value;
                    RaisePropertyChanged(TraceOptionProperty);
                }
            }
        }


        private void Log()
        {
            LogEntry clone = (LogEntry)logEntry.Clone();
            clone.Categories.Clear();
            if (GeneralOption) { clone.Categories.Add("General"); }
            if (DebugOption) { clone.Categories.Add("Debug"); }
            if (TraceOption) { clone.Categories.Add("Trace"); }
            clone.TimeStamp = DateTime.Now;
            Logger.Write(clone);
        }
    }
}
